package datastructure.stack;


import java.util.Stack;

/**
 * @author XY
 * @version 1.0
 * @date 2021/8/31 15:56
 * @Description 有效的括号
 */
public class ValidBrackets {

    public boolean isValid(String s){
        if(s.isEmpty()){
            return false;
        }
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if(c=='('||c=='['||c=='{'){
                stack.push(c);
            }else{
                if(stack.isEmpty()){
                    return false;
                }
                Character pop = stack.pop();
                if(c==')'&& pop != '('){
                    return false;
                }
                if(c==']'&& pop != '['){
                    return false;
                }
                if(c=='}'&& pop != '{'){
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
}
